package edu.aysf.controller;

import com.alibaba.druid.sql.visitor.functions.Now;
import edu.aysf.entity.OrderEntity;
import edu.aysf.service.OrderService;
import edu.aysf.service.impl.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

import static java.time.LocalTime.now;


@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;

    @RequestMapping("/byid")
    public List<OrderEntity> selectOrderById(@RequestBody Map<String,Object> map) {
        Integer user_id = Integer.parseInt(map.get("user_id").toString());
        return orderService.selectOrderById(user_id);
    }

    @RequestMapping("/all")
    public List<OrderEntity> selectOrder() {
        return orderService.selectOrder();
    }

    @RequestMapping("/create")
    public int createOrder(@RequestBody Map<String,Object> map) {
        Random random = new Random();
        int randomInt = random.nextInt(100000); // 生成一个0到999之间的随机整数
        String formatInt = String.format("%03d", randomInt);
        LocalDate currentDate = LocalDate.now();
        String id = "order-"+currentDate+"-"+formatInt;

//        int user_id = (int)map.get("user_id");
//        int goods_id = (int)map.get("goods_id");
//        int count = (int)map.get("count");

        Integer user_id = (Integer) map.get("user_id");
        Integer goods_id = (Integer) map.get("goods_id");
        Integer count = (Integer) map.get("count");
        System.out.println(user_id+":"+goods_id+":"+count);

        Date create_time = new Date();

        // 创建一个LocalDateTime实例
//        LocalDateTime localDateTime = LocalDateTime.now();
        // 将LocalDateTime转换为Timestamp
//        Timestamp create_time = Timestamp.valueOf(localDateTime);

        String isdelete = "0";
        return orderService.createOrder(id,user_id, goods_id, count, create_time,isdelete);
    }

    @RequestMapping("/reduce/count")
    public int updateGoodsCount(@RequestBody Map<String,Object> map) {
        int goods_id = Integer.parseInt(map.get("id").toString());
        int count = Integer.parseInt(map.get("count").toString());
        return orderService.updateGoodsCount(goods_id,count);
    }

    @RequestMapping("/reduce/money")
    public int updateAccountMoney(@RequestBody Map<String,Object> map) {
        int user_id = Integer.parseInt(map.get("user_id").toString());
        float money = Float.parseFloat(map.get("money").toString());
        return orderService.updateAccountMoney(user_id, money);
    }

    // 逻辑删除
    @RequestMapping("/delete")
    public int updateOrderCount(@RequestBody Map<String,Object> map) {
        String id = map.get("id").toString();
        String isdelete = map.get("isdelete").toString();
        return orderService.updateOrderCount(id, isdelete);
    }
}
